import {getCurrentUserInfo, getRoleMenu} from "../api/basisMG";
import {getStore, setStore} from "../utils/util";

const menuData = [
  {
    menuid: 1,
    icon: 'li-icon-xiangmuguanli',
    menuname: '服务管理',
    hasThird: null,
    url: null,
    show: false,
    menus: [
      {
        menuid: 11,
        icon: 'icon-cat-skuQuery',
        menuname: 'dubbo 服务',
        hasThird: 'N',
        url: 'service/Services',
        menus: null,
        show: false,

      },
      {
        menuid: 12,
        icon: 'icon-cat-skuQuery',
        menuname: 'api 信息',
        hasThird: 'N',
        url: 'service/ApiInfos',
        menus: null,
        show: false,
      }
    ]
  },
  {
    menuid: 2,
    icon: 'li-icon-dingdanguanli',
    menuname: '项目管理',
    hasThird: null,
    url: null,
    show: false,
    menus: [
      {
        menuid: 21,
        icon: 'icon-order-manage',
        menuname: '项目列表',
        hasThird: 'N',
        url: 'project/Project',
        menus: null,
        show: false
      },
      {
        menuid: 22,
        icon: 'icon-order-manage',
        menuname: '任务列表',
        hasThird: 'N',
        url: 'project/Task',
        menus: null,
        show: false
      }
    ]
  },
  {
    menuid: 3,
    icon: 'li-icon-dingdanguanli',
    menuname: '文件管理',
    hasThird: null,
    url: null,
    show: false,
    menus: [
      {
        menuid: 31,
        icon: 'icon-order-manage',
        menuname: '文件',
        hasThird: 'N',
        url: 'file/File',
        menus: null,
        show: false
      }
    ]
  },
  {
    menuid: 4,
    icon: 'li-icon-xitongguanli',
    menuname: '系统管理',
    hasThird: null,
    url: null,
    show: false,
    menus: [
      {
        menuid: 41,
        icon: 'icon-cus-manage',
        menuname: '用户管理',
        hasThird: 'N',
        url: 'system/user',
        menus: null,
        show: false,
      },
      {
        menuid: 42,
        icon: 'icon-cms-manage',
        menuname: '菜单管理',
        hasThird: 'N',
        url: 'system/Module',
        menus: null,
        show: false
      },
      {
        menuid: 43,
        icon: 'icon-news-manage',
        menuname: '角色管理',
        hasThird: 'N',
        url: 'system/Role',
        menus: null,
        show: false
      },
      {
        menuid: 44,
        icon: 'icon-cs-manage',
        menuname: '组织管理',
        hasThird: 'N',
        url: 'system/Organization',
        menus: null,
        show: false
      }
    ]
  },

  {
    menuid: 5,
    icon: 'li-icon-shangchengxitongtubiaozitihuayuanwenjian91',
    menuname: '图表',
    hasThird: null,
    url: null,
    show: false,
    menus: [
      {
        menuid: 51,
        icon: 'icon-provider-manage',
        menuname: '数据可视化',
        hasThird: 'N',
        url: 'charts/statistics',
        menus: null,
        show: false
      }
    ]
  }
]
const allMenuMap = new Map();
allMenuMap.set('dubbo_service', 11)
allMenuMap.set('api_info', 12)
allMenuMap.set('project_list', 21)
allMenuMap.set('task_list', 22)
allMenuMap.set('file_list', 31)
allMenuMap.set('user_list', 41)
allMenuMap.set('menu_manage', 42)
allMenuMap.set('role_manage', 43)
allMenuMap.set('org_manage', 44)
allMenuMap.set('data_view', 51)
export const loadMenuToLocalStorage = () => {
  let roleMenuName = []

  let userId = getStore('userId')
  if (userId == '' || userId == null) {
    setStore('accessToken', null);
    this.$router.push({path: '/login'})
  }
  let getUserInfo = {
    'accessToken': getStore('accessToken')
  }
  let requestData = {
    'requestData': JSON.stringify([getUserInfo])
  }
  let data = menuData
  getCurrentUserInfo(requestData).then(resp => {
    let menuIdSet = new Set();
    let roleId = resp.body.responseData.body.roleId;
    setStore('userInfoTmp', resp.body.responseData.body)
    getRoleMenu(roleId).then(resp => {
      let respList = resp.body;
      for (let i = 0; i < respList.length; i++) {
        roleMenuName.push(respList[i].url);
      }
      for (let i = 0; i < roleMenuName.length; i++) {
        let menuId = allMenuMap.get(roleMenuName[i]);
        menuIdSet.add(menuId);
      }
      let userFistPage = '';
      for (let i = 0; i < data.length; i++) {
        let showChild = false;
        for (let j = 0; j < data[i].menus.length; j++) {
          let id = data[i].menus[j].menuid;
          if (menuIdSet.has(id)) {
            data[i].menus[j].show = true;
            showChild = true;
            if (userFistPage == '') {
              userFistPage = data[i].menus[j].url;
              setStore('userFistPage', userFistPage);
            }
          }
        }
        data[i].show = showChild;
      }
      setStore('menuData', data)
    })
  })

}
